package org.tokaf.datasearcher;

import java.sql.*;
import java.util.ArrayList;
import java.util.Vector;

import org.tokaf.TopKElement;
import org.tokaf.normalizer.SimpleNormalizer;


public class MSSQLSearcher extends DataSearcher {
	ArrayList topK;

	int index = -1;

	String propertySearched;
	Connection conn;
	ResultSet set;
	public MSSQLSearcher() {
		;
	}

	public boolean needsSort() {
		return false;
	}

	public void sort(int index) {
		throw new UnsupportedOperationException();
	}
	

	public SimpleNormalizer getDefaultNormalizer() {
		return new SimpleNormalizer();
	}

	public Object clone() {
		throw new UnsupportedOperationException();
	}

	public int initSource(DataSearcher searcher) {
		long startTime = System.nanoTime();		
		try {
			// Load the Driver		
						   //
			Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); 
			conn = DriverManager.getConnection 
			  ("jdbc:microsoft:sqlserver://DWARF\\DWARF:1433;User="+userName+";Password="+password); 
			initSourceTime += System.nanoTime() - startTime;
			return 1;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return -1;
}
	public Object getField(int i) {

		try {
			return set.getObject(i);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

	public Object getFieldAtPosition(int index, int position) {
		/*TODO*/
		return null;
		//return getField(index, position);
	}

	public int advance() {
		try {
			if(set.next()==true){
				return set.getRow();
			}
			else
				return -1;
		} catch (SQLException e) {
				e.printStackTrace();
				return -1;
		}
	}

	public void initSearch(String query) {
		long startTime = System.nanoTime();
		try {
			PreparedStatement stm=conn.prepareStatement(query);
			stm.setFetchSize(1);
			stm.execute();
			set=stm.getResultSet();
			set.next();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		initSearchTime += System.nanoTime() - startTime;
	}

	public int initSource(String db) {
		long startTime = System.nanoTime();		
		try {
			// Load the Driver		
			//Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
		//	Class.forName("net.sourceforge.jtds.jdbc.Driver");
			
			conn = DriverManager.getConnection 
			  ("jdbc:microsoft:sqlserver://alan\\alan:0;DATABASENAME="+db+";User="+userName+";Password="+password);
			
			PreparedStatement stm=conn.prepareStatement("use "+db);
			stm.execute();
			initSourceTime += System.nanoTime() - startTime;
			return 1;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return -1;
	}

	public Object findObject(Object obj, int position, int index) {
		if (index == 1)
			return propertySearched;
		if (position == 0) {
			for (int i = 0; i < topK.size(); i++) {
				if (((TopKElement) topK.get(i)).name.equals(obj.toString())) {
					if (index == 2)
						return new Double(((TopKElement) topK.get(i)).rating);
				}

			}
		}
		return null;
	}

	public int getPosistion() {
		try {
			return set.getRow();
		} catch (SQLException e) {
			e.printStackTrace();
			return -1;
		}
	}

	public boolean hasNext() {
		try {
			if(set.next()){
				set.previous();
				return true;
			}
			else
				return false;
			//return !set.isLast();
		} catch (SQLException e) {			
			e.printStackTrace();
			return false;
		}
	}

	public Object next() {
		Vector v = new Vector();
		try {
			set.next();
			try{
				for(int i=1;;i++)
					v.add(set.getObject(i));
			}catch(SQLException e){
				
			}
			return v;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

	public void resetSearch() {
		try {
			set.first();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
